
'''
	抽象语法树基类
'''
class AST:
	pass;

# 定义程序节点
class Program(AST):
	# 程序由名称和语句块组成
	def __init__(self, name, block):
		self.name = name
		self.block = block

	def viewTree(self):
		return {
			'Program':{
					'name':self.name,
					'block':self.block.viewTree(),
				}
		}

# 定义语句块节点
class Block(AST):
	def __init__(self, import_file,declarations, compound_statement):  # 语句块由声明和符合语句组成
		self.import_file = import_file
		self.declarations = declarations
		self.compound_statement = compound_statement

	def viewTree(self):

		import_file_list=[];
		for index in range(len(self.import_file)):
			import_file_list.append(self.import_file[index].viewTree());

		declarations_list=[]
		for index in range(len(self.declarations)):
			declarations_list.append(self.declarations[index].viewTree());

		return {
			'Block':{
					'import_file':import_file_list,
					'declarations':declarations_list,
					'compound_statement':self.compound_statement.viewTree(),
				}
		}


# 添加复合语句节点
class Compound(AST):
	def __init__(self):
		self.children = []  # 子节点列表

	def viewTree(self):
		lists=[]
		for index in range(len(self.children)):

			# if type(self.children[index]).__name__=='NoOperator':
			# 	lists.append(self.children[index].viewTree());

			if type(self.children[index]).__name__ in ('Compound','Assign','PlusAssign','MinusAssign','MulAssign','DivAssign','If','For','Call','Print','Length'):
				lists.append(self.children[index].viewTree());

		return {
			'Compound':{'children':lists}
		}
'''
 #定义二元运算符节点类
 #加减乘除运算
'''
class BinaryOperator(AST):
	def __init__(self,left,operator,right):
		self.left=left;
		self.token=self.operator=operator;
		self.right=right;

	def viewTree(self):
		return {
			'BinaryOperator':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree()
			}
		}


# 定义一元运算符节点类(例：正负数)
class UnaryOperator(AST):
	def __init__(self,operator,expression):
		self.token=self.operator=operator;
		self.expression=expression;

	def viewTree(self):
		return {
			'UnaryOperator':{
				'token':self.token.viewTree(),
				'expression':self.expression.viewTree(),
			}
		}

# 比较运算
class ComparisonOperation(AST):
	def __init__(self,left,operator,right):
		self.left=left;
		self.token=self.operator=operator;
		self.right=right;

	def viewTree(self):
		return {
			'ComparisonOperation':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree()
			}
		}

# 布尔运算
class BooleanOperation(AST):
	def __init__(self,operator,right):
		self.token=self.operator=operator;
		self.value=token.value;

	def viewTree(self):
		return {
			'BooleanOperation':{
				'token':self.token.viewTree(),
				'value':self.value
			}
		}

#数字
class Num(AST):
	def __init__(self,token):
		self.token=token;
		self.value=token.value;

	def viewTree(self):
		return {
			'Num':{
				'value':self.value,
				'type':self.token.tokenType,
				# 'token':self.token.viewTree()
			}
		}

#字符串
class Str(AST):
	def __init__(self,token):
		self.token=token;
		self.value=token.value;

	def viewTree(self):
		return {
			'Str':{
				'value':self.value,
				'type':self.token.tokenType,
				# 'token':self.token.viewTree()
			}
		}

#布尔值
class Boolean(AST):
	def __init__(self,token):
		self.token=token;
		self.value=token.value;

	def viewTree(self):
		return {
			'Boolean':{
				'value':self.value,
				'type':self.token.tokenType,
				# 'token':self.token.viewTree()
			}
		}

# 添加变量节点
class Variable(AST):
	def __init__(self,token):
		self.token=token;
		self.name=token.value;

	def viewTree(self):
		return {
			'Variable':{
					'token':self.token.viewTree(),
					'name':self.name,
				}
		}

# 添加赋值语句节点
class Assign(AST):
	def __init__(self,left,operator,right):
		#变量名称
		self.left=left;
		# 记号和赋值符号
		self.token=self.operator=operator;
		# 右侧表达式
		self.right=right;

	def viewTree(self):
		obj= {
			'Assign':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree(),
			}
		}
		return obj;

# 添加加法赋值语句节点
class PlusAssign(AST):
	def __init__(self,left,operator,right):
		#变量名称
		self.left=left;
		# 赋值符号
		self.token=self.operator=operator;
		# 右侧表达式
		self.right=right;

	def viewTree(self):
		return {
			'PlusAssign':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree(),
			}
		}

# 添加减法赋值语句节点
class MinusAssign(AST):
	def __init__(self,left,operator,right):
		#变量名称
		self.left=left;
		# 赋值符号
		self.token=self.operator=operator;
		# 右侧表达式
		self.right=right;

	def viewTree(self):
		return {
			'MinusAssign':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree(),
			}
		}

# 添加乘法赋值语句节点
class MulAssign(AST):
	def __init__(self,left,operator,right):
		#变量名称
		self.left=left;
		# 赋值符号
		self.token=self.operator=operator;
		# 右侧表达式
		self.right=right;

	def viewTree(self):
		return {
			'MulAssign':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree(),
			}
		}

# 添加除法赋值语句节点
class DivAssign(AST):
	def __init__(self,left,operator,right):
		#变量名称
		self.left=left;
		# 赋值符号
		self.token=self.operator=operator;
		# 右侧表达式
		self.right=right;

	def viewTree(self):
		return {
			'DivAssign':{
				'left':self.left.viewTree(),
				'token':self.token.viewTree(),
				'right':self.right.viewTree(),
			}
		}

# 添加空语句节点
class NoOperator(AST):
	pass  # 无内容


# 定义变量声明节点
class VarDecl(AST):
	# 变量声明由变量和类型组成
	def __init__(self, var_node, type_node):
		self.var_node = var_node
		self.type_node = type_node

	def viewTree(self):
		return {
			'VarDecl':{
				'var_node':self.var_node.viewTree(),
				'type_node':self.type_node.viewTree(),
			}
		}

# 定义类型节点
class Type(AST):
	def __init__(self,token):
		self.token = token
		self.name = token.tokenType
		self.value = token.value

	def viewTree(self):
		return {
			'Type':{
					'token':self.token.viewTree(),
					'name':self.name,
				}
		}

# 逻辑非
class LogicWron(AST):
	def __init__(self,operator,expression):
		self.token=self.operator=operator;
		self.expression=expression;

	def viewTree(self):
		return {
			'LogicWron':{
				'token':self.token.viewTree(),
				'expression':self.expression.viewTree(),
			}
		}

# 文件导入
class Import(AST):
	def __init__(self,import_node,type_node):
		self.import_node=import_node;
		self.type_node=type_node;

	def viewTree(self):
		return {
			'Import':{
				'import_node':self.import_node.viewTree(),
				'type_node':self.type_node.viewTree(),
			}
		}

# 添加条件控制语句节点
class If(AST):
	def __init__(self,condition,then_node,else_node):
		# 判断条件
		self.condition=condition;
		# then节点
		self.then_node=then_node;
		# else节点
		self.else_node=else_node;

	def viewTree(self):
		if self.else_node =='':
			else_node=self.else_node
		else:
			else_node=self.else_node.viewTree()
		return {
			'If':{
				'condition':self.condition.viewTree(),
				'then_node':self.then_node.viewTree(),
				'else_node':else_node,
			}
		}

# 添加for循环控制语句节点
class For(AST):
	def __init__(self, start_node, end_node, do_node):
		self.start_node = start_node  # 起始节点
		self.end_node = end_node  # 终止节点
		self.do_node = do_node  # 循环体

	def viewTree(self):

		return {
			'For':{
				'start_node':self.start_node.viewTree(),
				'end_node':self.end_node.viewTree(),
				'do_node':self.do_node.viewTree(),
			}
		}

# 函数 参数节点
class Param(AST):
	def __init__(self, var_node, type_node):
		self.var_node=var_node    #参数变量名称
		self.type_node=type_node  #参数变量类型

	def viewTree(self):

		return {
			'Param':{
				'var_node':self.var_node.viewTree(),
				'type_node':self.type_node.viewTree(),
			}
		}

#函数调用节点
class Call(AST):
	def __init__(self, proc_name, actual_params, token):
		# 函数名称
		self.proc_name = proc_name
		# 函数实参列表
		self.actual_params = actual_params
		#函数token本身
		self.token = token
		#过程符号（里面主要存放了参数列表等信息）
		self.proc_symbol = None;

	def viewTree(self):
		actual_params_lists=[];
		for index in range(len(self.actual_params)):
			actual_params_lists.append(self.actual_params[index].viewTree());
		return {
			'Call':{
				'proc_name':self.proc_name,
				'actual_params':actual_params_lists,
				'token':self.token.viewTree(),
				'proc_symbol':'None',
			}
		}

# 添加函数声明节点
class Function(AST):
	def __init__(self, name, params, block_node,type_node):
		# 函数名称
		self.name = name
		#参数可能多个，因此是个列表
		self.params=params
		self.block_node = block_node  # 块节点
		#函数要比过程多一个特征值:返回数据类型
		self.type_node=type_node

	def viewTree(self):
		params_list=[];
		for index in range(len(self.params)):
			params_list.append(self.params[index].viewTree());
		return {
			'Function':{
				'name':self.name,
				'params':params_list,
				'block_node':self.block_node.viewTree(),
				'type_node':self.type_node.viewTree(),
			}
		}

# ======================== start 内置函数 ====================== #

# 屏幕打印节点
class Print(AST):
	def __init__(self, params):
		# print打印有多个参数，因此是个列表
		self.params=params;
	def viewTree(self):
		lists=[];
		for index in range(len(self.params)):
			lists.append(self.params[index].viewTree());
		return {
			'Print':lists
		}

# 获取字符串长度
class Length(AST):
	def __init__(self, params):
		self.params=params;

	def viewTree(self):
		return {
			'Length':self.params.viewTree()
		}

# 获取变量类型
class GetType(AST):
	def __init__(self, params):
		self.params=params;

	def viewTree(self):
		return {
			'Type':self.params.viewTree()
		}





# ======================== end 内置函数 ====================== #


